package edu.ucsd.cse110;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import edu.ucsd.cse110.model.Log;

/**
 * Displays a graph of WOD progression.
 */
public class StatsWodGraph {
	private static String wod = "";
	private TimeSeries series;
	private int wodId;
	private int wodType;

	/**
	 * Initializes local variables.
	 */
	public StatsWodGraph(String wod, int id) {
		StatsWodGraph.wod = wod;
		wodId = id;
	}

	/**
	 * Creates the graph of WOD progression.
	 */
	public Intent getIntent(Context context) {
		LocalDatabaseManager database = new LocalDatabaseManager(context);
		List<Log> wodList = database.getLogByWodId(wodId);
		database.close();

		if (wodList.size() > 0) {
			if ((wodList.get(0).getMinute() > 0)
					|| (wodList.get(0).getSecond() > 0)) {
				series = new TimeSeries(wod + " Times");
				wodType = 0;
				for (int i = 0; i < wodList.size(); i++) {
					series.add(i,
							((wodList.get(i).getMinute()) + ((double) (wodList
									.get(i).getSecond()) / 100)));
				}
			} else if (wodList.get(0).getRep() > 0) {
				series = new TimeSeries(wod + " Reps");
				wodType = 1;

				for (int i = 0; i < wodList.size(); i++) {
					series.add(i, (wodList.get(i).getRep()));
				}
			} else if (wodList.get(0).getRound() > 0) {
				series = new TimeSeries(wod + " Rounds");
				wodType = 2;

				for (int i = 0; i < wodList.size(); i++) {
					series.add(i, (wodList.get(i).getRound()));
				}
			}

			XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
			dataset.addSeries(series);

			XYSeriesRenderer renderer = new XYSeriesRenderer();
			renderer.setColor(Color.WHITE);
			renderer.setPointStyle(PointStyle.CIRCLE);
			renderer.setFillPoints(true);
			renderer.setChartValuesTextSize(25);
			renderer.setGradientEnabled(true);
			renderer.setDisplayChartValues(true);

			XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
			mRenderer.addSeriesRenderer(renderer);
			mRenderer.setBackgroundColor(Color.WHITE);
			mRenderer.setShowGrid(true);
			mRenderer.setGridColor(Color.GRAY);
			mRenderer.setChartTitle(wod);
			mRenderer.setChartTitleTextSize(30);
			mRenderer.setXTitle("Date");
			if (wodType == 0) {
				mRenderer.setYTitle("Time (minutes.seconds)");
			} else if (wodType == 1) {
				mRenderer.setYTitle("Reps");
			} else {
				mRenderer.setYTitle("Rounds");
			}
			mRenderer.setXLabels(0);
			Format formatter = new SimpleDateFormat("dd-MMM-yy");
			for (int i = 0; i < wodList.size(); i++) {
				mRenderer.addXTextLabel(i,
						formatter.format(wodList.get(i).getDate().getTime()));
			}
			mRenderer.setBarSpacing(20);
			mRenderer.setZoomButtonsVisible(true);
			Intent intent = ChartFactory.getLineChartIntent(context, dataset,
					mRenderer, "WOD Progression");

			return intent;
		}

		return null;
	}
}
